//
//  SearchListViewController.m
//  YiqiMobile
//
//  Created by henry on 13-6-11.
//  Copyright (c) 2013年 henry. All rights reserved.
//

#import "SearchListViewController.h"
#import "Utility.h"
#import "Config.h"
#import "DefineAction.h"
#import "JSON.h"
#import "MBProgressHUD.h"
#import "SearchListViewcell.h"
#import "BowenPreviewViewController.h"
#import "AppDelegate.h"
@interface SearchListViewController ()

@end

@implementation SearchListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.titleView=[Utility getTitleView:self.titlestr];
    self.arrayList=[NSMutableArray array];
    
	// Do any additional setup after loading the view.
    
    float tableHeight=UISCREEN_HEIGHT-64;
    CGRect tableViewFrame=CGRectMake(0, 0, UISCREEN_WIDTH, tableHeight);
    self.mytableView=[[UITableView alloc] initWithFrame:tableViewFrame];
    self.mytableView.tag=HOMEPAGETABLEVIEWTAG;
    self.mytableView.delegate=self;
    self.mytableView.dataSource=self;
    self.mytableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:self.mytableView];
    self.arrayList=[NSMutableArray array];
}
-(void) loadDataFromServer:(NSString *)keywords andType:(int)leixingId{
    self.leixingId=leixingId;
    AppDelegate *app=(AppDelegate*)[[UIApplication sharedApplication] delegate];
    if (app.status==NotReachable) {
        [Utility customAlertWithMsg:NETWORTERROE andTitle:ALRRTTITLE andDelagate:nil];
        return;
    }
    NSString *url=[NSString stringWithFormat:@"%@/%@",[Utility  getServerUrl],SEARCHBYSTRACTION];
    NSMutableDictionary *param=[NSMutableDictionary dictionaryWithDictionary:@{@"str":keywords,@"type":[NSString stringWithFormat:@"%d",leixingId]}];
    self.request=[[URLRequest alloc] initWithPostURL:url andDelegate:self andPostData:param andFile:nil];
    [[MBProgressHUD showHUDAddedTo:self.view animated:YES] setLabelText:LOADING_TITLE_DEFAULT];
    [self.request startLoadData];
}


- (void)requestFinished:(id)context{
    [MBProgressHUD hideAllHUDsForView:self.view animated:NO];
    @try{
        NSDictionary *result=[context JSONValue];
        if (result!=nil&&[[result  objectForKey:@"code"] integerValue]==0) {
            if ([[result  objectForKey:@"method"] isEqualToString:SEARCHBYSTRACTION]) {
                [self.arrayList removeAllObjects];
                [self.arrayList addObjectsFromArray:[result objectForKey:@"list"]];
                [self.mytableView reloadData];
            }
            
        }else{
            [Utility customAlertWithMsg:PROCESSERROE andTitle:ALRRTTITLE andDelagate:nil];
        }
    }@catch (NSException *e) {
        [Utility customAlertWithMsg:NETWORTERROE andTitle:ALRRTTITLE andDelagate:nil];
    }
}

- (void)requestFailed:(id)context error:(NSString *)error{
    [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:NO];
    [Utility customAlertWithMsg:NETWORTERROE andTitle:ALRRTTITLE andDelagate:nil];
}

//表视图委托
#pragma mark -
#pragma mark table view data source methods

//返回某个表视图有多少行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.arrayList count];
}

//设置行高度
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

//表视图显示表视图项时调用：第一次显示（根据视图大小显示多少个视图项就调用多少次）以及拖动时调用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *CellIdentifier = @"boweiListViewCell";
    
    SearchListViewcell *cell =(SearchListViewcell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[SearchListViewcell alloc] initWithStyle:UITableViewCellStyleDefault
                                        reuseIdentifier: CellIdentifier];
    }
    NSDictionary *dict=[self.arrayList objectAtIndex:indexPath.row];
    cell.titleLabel.text=[dict objectForKey:@"name"];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.leixingId==0) {
        BowenPreviewViewController *bowen=[[BowenPreviewViewController alloc] initWithNibName:nil bundle:nil];
        NSDictionary *dcit=[self.arrayList objectAtIndex:indexPath.row];
        [bowen loadDataFromServer:[dcit objectForKey:@"id"]];
        [self.navigationController pushViewController:bowen animated:YES];
    }
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
